#!/bin/bash
#
M_VER=3.6
M_INSTALL_VER=3.6.3
PORT=27017
BIND_IP=$(ifconfig eth0|grep inet|grep -v inet6|awk '{print $2}')
function yum_install_mongodb(){
	cd /etc/yum.repos.d 
	if [ ! -f mongodb-org-${M_VER}.repo ];then
		cat >>mongodb-org-${M_VER}.repo <<EOF
[mongodb-org-${M_VER}]
name = MongoDB Repository
baseurl = https://repo.mongodb.org/yum/redhat/7Server/mongodb-org/${M_VER}/x86_64/
gpgcheck = 0
enabled = 1
EOF
	fi
	yum install -y mongodb-org-${M_INSTALL_VER} mongodb-org-server-${M_INSTALL_VER} mongodb-org-shell-${M_INSTALL_VER} mongodb-org-mongos-${M_INSTALL_VER} mongodb-org-tools-${M_INSTALL_VER}
	cd /etc/ && [ ! -f mongod.conf.ori ] && cp /etc/mongod.conf{,.ori}
#修改配置文件
	 sed -i "s@bindIp: 127.0.0.1@bindIp: ${BIND_IP}@g" /etc/mongod.conf 
#启动mongod服务
	systemctl start mongod 
	if [ "`ss -tunl|grep ${PORT}|cut -d: -f2|awk '{print $1}'`" == "${PORT}" ];then
		echo "mongod start successful"
	else
		echo "mongod start fail"
	fi
}
main(){
	yum_install_mongodb
}
main
